$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'CourseAttendanceState/Select',
        datatype: "json",
        colModel: [
            { label: '班级id', name: 'classId', width: 40,key: true,hidden:true },
             { label: '班级名称', name: 'className', width: 40 },
             { label: '课程名称', name: 'courseName', width: 40 },
            { label: '日期', name: 'courseDate', width: 40 },
            { label: '第几节', name: 'courseIndex', width: 40 },
            { label: '课堂考勤状态', name: 'courseAttendanceState', width: 40, formatter: function(value, options, row){
                return value == 0 ?
                    '<span class="table-btn1 isNot">未填</span>' :
                    '<span class="table-btn1 isSure">已填</span>';
            }},
            { label: '是否开放补填', name: 'courseAttendanceModified', width: 40, formatter: function(value, options, row){
                return value == 0 ?
                    '<span class="table-btn1 isNot">未开放</span>' :
                    '<span class="table-btn1 isSure">已开放</span>';
            }}
         ],
        viewrecords: true,
        height: '100%',
        rowNum: 10,
        rowList : [10,30,50],
        // rownumbers: true,
        // rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        CourseAttendanceState: {},
        classId: null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.CourseAttendanceState = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "CourseAttendanceState/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.CourseAttendanceState = r.CourseAttendanceState;
                vm.classId = r.CourseAttendanceState.classId;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "CourseAttendanceState/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.CourseAttendanceState.classId == vm.classId ? "CourseAttendanceState/Update" : "CourseAttendanceState/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.CourseAttendanceState),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'className': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.CourseAttendanceState.classId)){
                alert("班级id不能为空");
                return true;
            }
            if(isBlank(vm.CourseAttendanceState.className)){
                alert("班级名称不能为空");
                return true;
            }
            if(isBlank(vm.CourseAttendanceState.courseName)){
                alert("课程名称不能为空");
                return true;
            }
            if(isBlank(vm.CourseAttendanceState.courseAttendanceState)){
                alert("课堂考勤状态不能为空");
                return true;
            }
            if(isBlank(vm.CourseAttendanceState.courseAttendanceModified)){
                alert("是否已补填不能为空");
                return true;
            }
            }
        }
});